package com.province.platform.common.utils;


import com.province.platform.common.enums.PlatformErrorCode;
import com.province.platform.common.exception.PlatformException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.net.Socket;

public class NetUtils {
    private static final Logger LOG = LoggerFactory.getLogger(NetUtils.class);

    private NetUtils() {}

    /**
     * 检测连接是否可用
     * @param url url
     */
    public static void testConnection(String url) {
        String[] parts = url.split(":");
        String ip = parts[0];
        String port = parts[1];

        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(ip, Integer.parseInt(port)));
        } catch (Exception e) {
            String message = String.format("telnet服务器连接失败: [%s]", "message: ip =" + ip + ",port =" + port);
            LOG.error(message);
            throw PlatformException.asPlatformException(PlatformErrorCode.FAIL_CONNECT, message, e);
        }
    }
}
